package com.exartisan.thanhdoanhn;
import java.text.SimpleDateFormat;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.VideoView;

import com.exartisan.bean.GalleryContent;
import com.exartisan.util.CommentDialog;
import com.exartisan.util.DataHolder;

public class VideoActivity extends Activity {

	private static boolean visible = true;
	private static boolean isPaused = false;
	private static boolean videoLiked = false;
	private ImageButton btnVideoLike;
	private TextView tvVideoLikes;
	private FrameLayout layoutUtil;
	private VideoView video;
	private SeekBar seekBar;
	private TextView tvDuration;
	private TextView tvCurrentProgress;
	private TextView tvTitle;
	private ImageButton btnPause;
	private Handler mHandler = new Handler();
	private Runnable onEverySecond = new Runnable() {
		@Override
		public void run() {
			if (seekBar != null && video.isPlaying()) {
				int progress = video.getCurrentPosition();
				seekBar.setProgress(progress);
				tvCurrentProgress.setText(convertToTime(progress));
			}
		}
	};
	@SuppressLint("NewApi") @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		
		setContentView(R.layout.activity_video);
		
		btnVideoLike = (ImageButton) findViewById(R.id.btnVideoLike);
		tvVideoLikes = (TextView) findViewById(R.id.tvVideoLikes);
		
		layoutUtil = (FrameLayout) findViewById(R.id.layout_util);
		btnPause = (ImageButton) findViewById(R.id.btnPause);
		tvCurrentProgress = (TextView) findViewById(R.id.tvCurrentProgess);
		tvDuration = (TextView) findViewById(R.id.tvDuration);
		
		tvTitle =(TextView) findViewById(R.id.tvTitle);
		seekBar = (SeekBar) findViewById(R.id.seekBar);
		video = (VideoView) findViewById(R.id.video);
		GalleryContent content = getIntent().getParcelableExtra("data");
		tvTitle.setText(content.getDescription());
		Uri videoPath = Uri.parse(content.getResourceUrl());
//		Uri videoPath = Uri.parse("android.resource://" + getPackageName()
//				+ "/" + R.raw.big_buck_bunny);
		video.setVideoURI(videoPath);
		video.requestFocus();
		video.start();
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				if (fromUser) {
					// this is when actually seekbar has been seeked to a new
					// position
					if(video.isPlaying()==false) video.start();
					video.seekTo(progress);
					tvCurrentProgress.setText(convertToTime(progress));
				}
			}
		});
		video.setOnPreparedListener(new OnPreparedListener() {
			@Override
			public void onPrepared(MediaPlayer mp) {
				int duration =video.getDuration();
				seekBar.setMax(duration);
				tvDuration.setText(convertToTime(duration));
//				seekBar.postDelayed(onEverySecond, 1000);
				Thread thread = new Thread(new Runnable() {
					
					@Override
					public void run() {
						while (true) {
							try {
								Thread.sleep(1000);
								mHandler.post(onEverySecond);	
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							
						}
						
					}
				});
				thread.start();
			}
		});
		btnPause.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int progress = video.getCurrentPosition();
				if (video.isPlaying()) {			
					video.pause();
					btnPause.setImageResource(R.drawable.ic_play);
				} else {
					btnPause.setImageResource(R.drawable.ic_pause);
					video.start();
					video.seekTo(progress);
				}
			}
		});
		
		setAutoHide();
		
	}
	
	public void onClickScreen(View view){
		if (visible == false) {
			layoutUtil.setVisibility(View.GONE);
			visible = true;
		} else if(visible == true) {
			layoutUtil.setVisibility(View.VISIBLE);
			setAutoHide();
			visible = false;
		}
	}
	
	private void setAutoHide(){
		Thread thread = new Thread(new Runnable() {
			
			@Override
			public void run() {
					try {
						Thread.sleep(5000);
						mHandler.post(new Runnable() {
							
							@Override
							public void run() {
								layoutUtil.setVisibility(View.GONE);
							}
						});	
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
		});
		thread.start();
	}
	
	private String convertToTime(int milisecond) {
		if (milisecond / 3600000 == 0)
			return new SimpleDateFormat("mm:ss").format(milisecond);
		else
			return new SimpleDateFormat("HH:mm:ss").format(milisecond);
	}
	
	public void onClickLikeVideo(View view){
		if(videoLiked==false){
			tvVideoLikes.setTextColor(Color.parseColor("#bf0000"));
			btnVideoLike.setImageResource(R.drawable.like_red);
			videoLiked = true;
		}
		else {
			tvVideoLikes.setTextColor(Color.parseColor("#ffffff"));
			btnVideoLike.setImageResource(R.drawable.like_white);
			videoLiked = true;
		}
	}
	
	public void onClickCommentVideo(View view){
		CommentDialog.show(this, DataHolder.FEED_ID_SAMPLE_2);
	}
	
	
}